<?php
class GalleryController extends GalleryAppController {
  	var $name = 'Gallery';
	var $uses = array('Upload','Gallery','GalleryCategory');
	
	var $permissions = array(
		'add' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag')
	);


	public function beforeFilter(){
		parent::beforeFilter();	
		$this->layout = 'gallery';	
		$this->FileUpload->fileModel('Upload');
		$this->FileUpload->uploadDir(UPLOAD_PATH);
	}
	
	function add_category(){
		if(!empty($this->data)){

			$this->data['GalleryCategory']['slug'] = Inflector::slug($this->data['GalleryCategory']['name'],'-');
			$lp = $this->GalleryCategory->find('first',array('fields'=>array('MAX(GalleryCategory.lp) AS lp')));
			$this->data['GalleryCategory']['lp'] = $lp['0']['lp']+1;
			$this->data['GalleryCategory']['menu'] = 1;
			$this->GalleryCategory->create();
			if ($this->GalleryCategory->save($this->data)) {
				$this->Session->setFlash(__('Kategoria dodana pomylnie.', true));
			} else {
				$this->Session->setFlash(__('Błd zapisu nowej kategorii', true));
			}
			$this->redirect($this->referer());
		}
		
	}
	
	function add($category_id=null){
		if(!empty($this->data)){
			if($this->FileUpload->success){
				
				$this->data['Gallery']['upload_id'] = $this->Upload->getLastInsertID();
				$this->data['Gallery']['added'] = date('Y-m-d H:i:s');
				$this->data['Gallery']['slug'] = Inflector::slug($this->data['Gallery']['name'],'-');
				$lp = $this->Gallery->find('first',array('conditions'=>array('Gallery.category_id'=>$this->data['Gallery']['category_id']),'fields'=>array('MAX(Gallery.lp) AS lp'), 'group' => 'Gallery.category_id'));
				$this->data['Gallery']['lp'] = $lp['0']['lp']+1;
				
				$this->Gallery->create();
				if ($this->Gallery->save($this->data)) {
					$this->Session->setFlash(__('The file :'.print_r($this->FileUpload->currentFile['name'],true).' has been saved', true));
				} else {
					$this->Session->setFlash(__('The picture could not be saved. Please, try again.', true));
				}
								 
			}else{
				$this->Session->setFlash($this->FileUpload->showErrors().'.');
			}
			$this->redirect($this->referer());
		}
		$this->data['Gallery']['category_id'] = (is_null($category_id)) ? $this->data['Gallery']['category_id']:$category_id;
	}
	
	function edit($id=null){
		if(!empty($this->data)){
			$this->data['Gallery']['slug'] = Inflector::slug($this->data['Gallery']['name'],'-');
			if ($this->Gallery->save($this->data)) {
				$this->Session->setFlash(__('The picture has been saved', true));
			} else {
				$this->Session->setFlash(__('The picture could not be saved. Please, try again.', true));
			}	
			$this->redirect($this->referer());			
		}
		$this->data = $this->Gallery->findById($id);
	}

	function view($id=null){
		$this->layout = 'default';
		$this->data = $this->Gallery->findBySlug($id);
	}	
	
	function about($id=null){
		$this->layout = 'default';
		$this->data = $this->Gallery->findBySlug($id);
	}	

	function work($id=null){
		$this->layout = 'default';
		$this->data = $this->Gallery->findBySlug($id);
	}
	
	function delete($id = null) {
		$gallery = $this->Gallery->findById($id);
	    $upload =  $this->Upload->findById($gallery['Gallery']['upload_id']);	    
	    if($this->FileUpload->removeFile($upload['Upload']['name'])){
			if($this->Upload->delete($upload['Upload']['id'])){
		      	if($this->Gallery->delete($gallery['Gallery']['id'])){
		        	$this->Session->setFlash('File deleted');
		      	}else{
		      		$this->Session->setFlash(__('The picture could not be deleted from Gallery. Please, try again.', true));
		      	}
	        	
			}else{
				$this->Session->setFlash(__('The picture could not be deleted from Upload. Please, try again.', true));
			}
	    }else{
	    	$this->Session->setFlash(__('The picture could not be deleted from Disc. Please, try again.', true));
	    }
	    $this->redirect($this->referer());
	}
	
	function delete_gallery($category_id = null) {
		$gallery = $this->Gallery->find('list',array(
			'conditions'=>array('Gallery.category_id'=>$category_id),
			'fields'=>array('Gallery.id','Gallery.upload_id')));
		foreach ($gallery as $gallery_id => $upload_id) {
			$upload =  $this->Upload->findById($upload_id);	    
		    if($this->FileUpload->removeFile($upload['Upload']['name'])){
				if($this->Upload->delete($upload['Upload']['id'])){
			      	if($this->Gallery->delete($gallery_id)){
			        	$this->Session->setFlash('File deleted');
			      	}else{
			      		$this->Session->setFlash(__('The picture could not be deleted from Gallery. Please, try again.', true));
			      	}
		        	
				}else{
					$this->Session->setFlash(__('The picture could not be deleted from Upload. Please, try again.', true));
				}
		    }else{
		    	$this->Session->setFlash(__('The picture could not be deleted from Disc. Please, try again.', true));
		    }
		    
		}	
		$this->GalleryCategory->delete($category_id);
	    $this->redirect($this->referer());
	}	
	
	function up($id=null){
		
		$a = $this->Gallery->read(null, $id);
		$b = $this->Gallery->find('first',array(
	        'conditions' => array(
				'Gallery.category_id = ' =>$a['Gallery']['category_id'],
				'Gallery.lp = ' => $a['Gallery']['lp'] - 1				
			)));
				
		if(is_array($a) && !empty($a)){
			$a['Gallery']['lp'] = (isset($a['Gallery']['lp']) && $a['Gallery']['lp'] > 1 ) ? $a['Gallery']['lp'] - 1 : 1;
			$this->Gallery->save($a);
			if(is_array($b) && !empty($b)){
				$b['Gallery']['lp'] = (isset($b['Gallery']['lp']) ) ? $b['Gallery']['lp'] + 1 : 0;
				$this->Gallery->save($b);	
			}			
		}
		$this->redirect($this->referer());
	}	
	
	function down($id=null){
				
		$a = $this->Gallery->read(null, $id);		
		$b = $this->Gallery->find('first',array(
	        'conditions' => array(
				'Gallery.category_id = ' =>$a['Gallery']['category_id'],
				'Gallery.lp = ' => $a['Gallery']['lp'] + 1				
			)));
	
		if(is_array($a) && !empty($a)){
			$a['Gallery']['lp'] = (isset($a['Gallery']['lp']) ) ? $a['Gallery']['lp'] + 1 : 0;		
			$this->Gallery->save($a);
			if(is_array($b) && !empty($b)){
				$b['Gallery']['lp'] = (isset($b['Gallery']['lp']) ) ? $b['Gallery']['lp'] - 1 : 0;
				$this->Gallery->save($b);	
			}			
		}
		$this->redirect($this->referer());
	}
}
?>